#include "modern_icons.h"
#include "icon_design_specs.h"
#include <QPainter>
#include <QApplication>
#include <QStyle>

ModernIcons::ModernIcons(QObject* parent)
    : QObject(parent)
    , m_exportMode(SeparateMode)
    , m_themeColor("#007AFF")
    , m_isDarkMode(false)
    , m_baseSize(IconDesignSpecs::BASE_PIXMAP_SIZE)
{
}

QIcon ModernIcons::createImportIcon(const QColor& themeColor)
{
    return createAdaptiveIcon(
        [this](QPainter& painter, qreal scale, const QColor& color) {
            drawImportIcon(painter, scale, color);
        }, themeColor);
}

QIcon ModernIcons::createExportIcon(ExportMode mode, const QColor& themeColor)
{
    return createAdaptiveIcon(
        [this, mode](QPainter& painter, qreal scale, const QColor& color) {
            if (mode == SeparateMode) {
                drawSeparateExportIcon(painter, scale, color);
            } else {
                drawMergedExportIcon(painter, scale, color);
            }
        }, themeColor);
}

QIcon ModernIcons::createThemedImportIcon()
{
    return createImportIcon(getAdaptiveThemeColor());
}

QIcon ModernIcons::createThemedExportIcon(ExportMode mode)
{
    return createExportIcon(mode, getAdaptiveThemeColor());
}

QIcon ModernIcons::createAdaptiveIcon(std::function<void(QPainter&, qreal, const QColor&)> drawFunc,
                                    const QColor& themeColor)
{
    QIcon adaptiveIcon;

    // 生成多个尺寸变体
    QList<int> sizes = {
        IconDesignSpecs::SMALL_SIZE,
        IconDesignSpecs::MEDIUM_SIZE,
        IconDesignSpecs::LARGE_SIZE,
        IconDesignSpecs::XLARGE_SIZE
    };

    for (int size : sizes) {
        QPixmap pixmap(size, size);
        pixmap.fill(Qt::transparent);

        QPainter painter(&pixmap);
        painter.setRenderHint(QPainter::Antialiasing, true);
        painter.setRenderHint(QPainter::SmoothPixmapTransform, true);

        // 计算缩放比例
        qreal scale = static_cast<qreal>(size) / static_cast<qreal>(m_baseSize);

        // 绘制图标
        IconDesignSpecs::setupPainter(painter, themeColor, IconDesignSpecs::MAIN_STROKE_WIDTH * scale);
        drawFunc(painter, scale, themeColor);

        adaptiveIcon.addPixmap(pixmap);
    }

    return adaptiveIcon;
}

QColor ModernIcons::getAdaptiveThemeColor() const
{
    QColor baseColor = m_themeColor;

    if (m_isDarkMode) {
        // 深色主题使用更亮的颜色
        return baseColor.lighter(130);
    } else {
        // 浅色主题使用标准颜色
        return baseColor;
    }
}

QString ModernIcons::exportModeText(ExportMode mode)
{
    switch (mode) {
        case SeparateMode:
            return QObject::tr("分别导出PDF");
        case MergedMode:
            return QObject::tr("合并导出PDF");
        default:
            return QObject::tr("导出PDF");
    }
}

QString ModernIcons::exportModeDescription(ExportMode mode)
{
    switch (mode) {
        case SeparateMode:
            return QObject::tr("每个文件导出为一个独立的PDF文件");
        case MergedMode:
            return QObject::tr("所有文件合并导出为一个PDF文件");
        default:
            return QObject::tr("导出PDF文件");
    }
}

QIcon ModernIcons::createIconVariant(const QIcon& baseIcon, const QColor& color, QIcon::Mode mode)
{
    QIcon variant;
    QList<QSize> availableSizes = baseIcon.availableSizes();

    for (const QSize& size : availableSizes) {
        QPixmap pixmap = baseIcon.pixmap(size);
        if (!pixmap.isNull()) {
            // 重新着色
            QPainter painter(&pixmap);
            painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
            painter.fillRect(pixmap.rect(), color);
            painter.end();

            variant.addPixmap(pixmap, mode);
        }
    }

    return variant;
}

// 基础绘制函数实现
void ModernIcons::drawImportIcon(QPainter& painter, qreal scale, const QColor& color)
{
    // 应用统一的设计规范
    IconDesignSpecs::setupPainter(painter, color, IconDesignSpecs::MAIN_STROKE_WIDTH * scale);

    const int margin = IconDesignSpecs::MARGIN * scale;
    const int spacing = IconDesignSpecs::GRID_UNIT * scale;
    const qreal cornerRadius = IconDesignSpecs::CORNER_RADIUS * scale;
    const qreal mainStroke = IconDesignSpecs::MAIN_STROKE_WIDTH * scale;
    const qreal detailStroke = IconDesignSpecs::DETAIL_STROKE_WIDTH * scale;

    QColor mainColor = color;
    QColor detailColor = IconDesignSpecs::getDetailColor(color);

    // 文件文档 - 黄金比例尺寸
    qreal fileWidth = spacing * 8;   // 32px基础
    qreal fileHeight = spacing * 10; // 40px基础
    QRectF fileRect(margin, margin + spacing * 2, fileWidth, fileHeight);

    painter.setPen(QPen(mainColor, mainStroke, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
    painter.drawRoundedRect(fileRect, cornerRadius, cornerRadius);

    // 文件内容线 - 基于网格系统的完美布局
    painter.setPen(QPen(detailColor, detailStroke, Qt::SolidLine, Qt::RoundCap));
    qreal lineStartX = margin + spacing * 1.5;
    qreal lineEndX = margin + spacing * 6.5;
    qreal lineY1 = margin + spacing * 4;
    qreal lineY2 = margin + spacing * 6;
    qreal lineY3 = margin + spacing * 8;

    painter.drawLine(QPointF(lineStartX, lineY1), QPointF(lineEndX, lineY1));
    painter.drawLine(QPointF(lineStartX, lineY2), QPointF(lineEndX, lineY2));
    painter.drawLine(QPointF(lineStartX, lineY3), QPointF(lineEndX, lineY3));

    // 完美几何的向下箭头
    painter.setPen(QPen(mainColor, mainStroke, Qt::SolidLine, Qt::RoundCap));
    qreal arrowCenterX = margin + spacing * 4.5;
    qreal arrowTop = margin;
    qreal arrowBottom = margin + spacing * 2;
    qreal arrowHalfWidth = spacing * 1.5;

    painter.drawLine(QPointF(arrowCenterX, arrowTop), QPointF(arrowCenterX, arrowBottom));
    painter.drawLine(QPointF(arrowCenterX, arrowBottom), QPointF(arrowCenterX - arrowHalfWidth, arrowBottom - arrowHalfWidth));
    painter.drawLine(QPointF(arrowCenterX, arrowBottom), QPointF(arrowCenterX + arrowHalfWidth, arrowBottom - arrowHalfWidth));
}

void ModernIcons::drawSeparateExportIcon(QPainter& painter, qreal scale, const QColor& color)
{
    IconDesignSpecs::setupPainter(painter, color, IconDesignSpecs::MAIN_STROKE_WIDTH * scale);

    const int margin = IconDesignSpecs::MARGIN * scale;
    const int spacing = IconDesignSpecs::GRID_UNIT * scale;
    const qreal cornerRadius = IconDesignSpecs::CORNER_RADIUS * scale;
    const qreal mainStroke = IconDesignSpecs::MAIN_STROKE_WIDTH * scale;
    const qreal detailStroke = IconDesignSpecs::DETAIL_STROKE_WIDTH * scale;
    const qreal fineStroke = IconDesignSpecs::FINE_STROKE_WIDTH * scale;

    QColor primaryColor = color;
    QColor secondaryColor = IconDesignSpecs::getTertiaryColor(color);

    // 左侧PDF文件
    painter.setPen(QPen(secondaryColor, mainStroke, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
    QRectF leftFile(margin, margin + spacing * 2, spacing * 7, spacing * 9);
    painter.drawRoundedRect(leftFile, cornerRadius, cornerRadius);

    // 右侧PDF文件 - 完美镜像对称
    QRectF rightFile(margin + spacing * 9, margin + spacing * 2, spacing * 7, spacing * 9);
    painter.drawRoundedRect(rightFile, cornerRadius, cornerRadius);

    // 统一的PDF文字
    QFont unifiedFont = IconDesignSpecs::getUnifiedFont(IconDesignSpecs::FONT_SIZE_MEDIUM * scale);
    painter.setFont(unifiedFont);

    painter.drawText(leftFile.adjusted(spacing, spacing, -spacing, -spacing), Qt::AlignCenter, "PDF");
    painter.drawText(rightFile.adjusted(spacing, spacing, -spacing, -spacing), Qt::AlignCenter, "PDF");

    // 完美对称的分离导出箭头
    painter.setPen(QPen(primaryColor, mainStroke, Qt::SolidLine, Qt::RoundCap));

    qreal leftArrowX = margin + spacing * 3.5;
    qreal rightArrowX = margin + spacing * 11.5;
    qreal arrowY1 = margin + spacing * 11;
    qreal arrowY2 = margin + spacing * 13;
    qreal arrowHalfWidth = spacing * 1.5;

    // 左侧箭头
    painter.drawLine(QPointF(leftArrowX, arrowY1), QPointF(leftArrowX, arrowY2));
    painter.drawLine(QPointF(leftArrowX, arrowY2), QPointF(leftArrowX - arrowHalfWidth, arrowY2 - arrowHalfWidth));
    painter.drawLine(QPointF(leftArrowX, arrowY2), QPointF(leftArrowX + arrowHalfWidth, arrowY2 - arrowHalfWidth));

    // 右侧箭头 - 完美镜像
    painter.drawLine(QPointF(rightArrowX, arrowY1), QPointF(rightArrowX, arrowY2));
    painter.drawLine(QPointF(rightArrowX, arrowY2), QPointF(rightArrowX - arrowHalfWidth, arrowY2 - arrowHalfWidth));
    painter.drawLine(QPointF(rightArrowX, arrowY2), QPointF(rightArrowX + arrowHalfWidth, arrowY2 - arrowHalfWidth));

    // 分离标识虚线 - 完美居中
    painter.setPen(QPen(primaryColor, fineStroke, Qt::DashLine, Qt::RoundCap));
    painter.drawLine(QPointF(margin + spacing * 7.5, margin + spacing),
                    QPointF(margin + spacing * 7.5, margin + spacing * 3));
}

void ModernIcons::drawMergedExportIcon(QPainter& painter, qreal scale, const QColor& color)
{
    IconDesignSpecs::setupPainter(painter, color, IconDesignSpecs::MAIN_STROKE_WIDTH * scale);

    const int margin = IconDesignSpecs::MARGIN * scale;
    const int spacing = IconDesignSpecs::GRID_UNIT * scale;
    const qreal cornerRadius = IconDesignSpecs::CORNER_RADIUS * scale;
    const qreal mainStroke = IconDesignSpecs::MAIN_STROKE_WIDTH * scale;
    const qreal detailStroke = IconDesignSpecs::DETAIL_STROKE_WIDTH * scale;

    QColor primaryColor = color;
    QColor bottomColor = IconDesignSpecs::getTertiaryColor(color);
    QColor topColor = primaryColor;

    // 底层文件 - 精确层叠位置
    painter.setPen(QPen(bottomColor, mainStroke, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
    QRectF bottomFile(margin + spacing * 2, margin + spacing * 3, spacing * 7, spacing * 9);
    painter.drawRoundedRect(bottomFile, cornerRadius, cornerRadius);

    // 顶层文件 - 完美层叠偏移
    QRectF topFile(margin, margin + spacing, spacing * 7, spacing * 9);
    painter.drawRoundedRect(topFile, cornerRadius, cornerRadius);

    // 统一的PDF文字（仅顶层显示）
    QFont unifiedFont = IconDesignSpecs::getUnifiedFont(IconDesignSpecs::FONT_SIZE_MEDIUM * scale);
    painter.setFont(unifiedFont);
    painter.drawText(topFile.adjusted(spacing, spacing, -spacing, -spacing), Qt::AlignCenter, "PDF");

    // 完美几何的汇聚箭头
    painter.setPen(QPen(primaryColor, mainStroke, Qt::SolidLine, Qt::RoundCap));

    qreal centerX = margin + spacing * 5.5;
    qreal convergeY = margin + spacing * 11;
    qreal exportY = margin + spacing * 13;
    qreal arrowHalfWidth = spacing * 1.5;

    // 左汇聚线
    painter.drawLine(QPointF(margin + spacing * 3, convergeY), QPointF(centerX, exportY));
    // 右汇聚线
    painter.drawLine(QPointF(margin + spacing * 8, convergeY), QPointF(centerX, exportY));
    // 向上导出主线
    painter.drawLine(QPointF(centerX, exportY), QPointF(centerX, margin + spacing * 15));
    // 箭头头部 - 完美对称
    painter.drawLine(QPointF(centerX, margin + spacing * 15),
                    QPointF(centerX - arrowHalfWidth, margin + spacing * 15 - arrowHalfWidth));
    painter.drawLine(QPointF(centerX, margin + spacing * 15),
                    QPointF(centerX + arrowHalfWidth, margin + spacing * 15 - arrowHalfWidth));

    // 合并中心点 - 完美圆形
    painter.setBrush(QBrush(primaryColor));
    painter.drawEllipse(QPointF(centerX, exportY), spacing * 0.6, spacing * 0.6);

    // 加号标识 - 完美十字
    painter.setBrush(Qt::NoBrush);
    painter.setPen(QPen(primaryColor, detailStroke, Qt::SolidLine, Qt::RoundCap));
    painter.drawLine(QPointF(centerX, exportY - spacing * 0.8), QPointF(centerX, exportY + spacing * 0.8));
    painter.drawLine(QPointF(centerX - spacing * 0.8, exportY), QPointF(centerX + spacing * 0.8, exportY));
}

// 辅助绘制函数
void ModernIcons::drawFileDocument(QPainter& painter, const QRectF& rect, const QColor& color, qreal scale)
{
    IconDesignSpecs::setupPainter(painter, color, IconDesignSpecs::MAIN_STROKE_WIDTH * scale);

    qreal cornerRadius = IconDesignSpecs::CORNER_RADIUS * scale;
    painter.drawRoundedRect(rect, cornerRadius, cornerRadius);
}

void ModernIcons::drawPdfDocument(QPainter& painter, const QRectF& rect, const QColor& color, qreal scale)
{
    // PDF文档特殊绘制 - 带折角效果
    IconDesignSpecs::setupPainter(painter, color, IconDesignSpecs::MAIN_STROKE_WIDTH * scale);

    qreal cornerRadius = IconDesignSpecs::CORNER_RADIUS * scale;

    // 文档主体
    painter.drawRoundedRect(rect, cornerRadius, cornerRadius);

    // 文档折角
    QPainterPath foldPath;
    qreal foldSize = rect.width() * 0.3;
    foldPath.moveTo(rect.right(), rect.top());
    foldPath.lineTo(rect.right(), rect.top() + foldSize);
    foldPath.lineTo(rect.right() + foldSize * 0.7, rect.top() + foldSize);
    painter.drawPath(foldPath);
}

void ModernIcons::drawArrow(QPainter& painter, const QPointF& start, const QPointF& end,
                          const QColor& color, qreal scale)
{
    IconDesignSpecs::setupPainter(painter, color, IconDesignSpecs::MAIN_STROKE_WIDTH * scale);
    IconDesignSpecs::drawArrow(painter, start, end, IconDesignSpecs::ARHEAD_SIZE * scale);
}

void ModernIcons::drawConvergeArrows(QPainter& painter, const QPointF& center, const QColor& color, qreal scale)
{
    IconDesignSpecs::setupPainter(painter, color, IconDesignSpecs::MAIN_STROKE_WIDTH * scale);

    qreal spacing = IconDesignSpecs::GRID_UNIT * scale;
    qreal arrowLength = spacing * 1.5;

    // 左汇聚箭头
    QPointF leftStart(center.x() - spacing * 2, center.y() - spacing);
    QPointF leftEnd(center.x(), center.y());
    IconDesignSpecs::drawArrow(painter, leftStart, leftEnd, spacing * 0.8);

    // 右汇聚箭头
    QPointF rightStart(center.x() + spacing * 2, center.y() - spacing);
    QPointF rightEnd(center.x(), center.y());
    IconDesignSpecs::drawArrow(painter, rightStart, rightEnd, spacing * 0.8);

    // 向上导出箭头
    QPointF upStart(center.x(), center.y());
    QPointF upEnd(center.x(), center.y() + spacing * 2);
    IconDesignSpecs::drawArrow(painter, upStart, upEnd, spacing * 0.8);
}